/*---------------------------------------------------------------------
 * File name: ssd1306.cpp
 *
 * Copyright (c) <2020-2022>, <ChenLong>
 *
 * All rights reserved.
 *
 * Author: ChenLong
 * Email: worldlong@foxmail.com
 *--------------------------------------------------------------------*/
#include "ZJY096S0700BG01.h"
/*
*/
void ZJY096S0700BG01::write_cmd(uint8_t cmd)
{
  _dc.write(0);
  _cs.write(0);
  _spi.write(&cmd, 1);
  _cs.write(1);
  _timer.delay_us(1);
}
/*
*/
void ZJY096S0700BG01::write_data(uint8_t data)
{
  _dc.write(1);
  _cs.write(0);
  _spi.write(&data, 1);
  _cs.write(1);
  _timer.delay_us(1);
}
/*
*/
void ZJY096S0700BG01::reset()
{
  _reset.write(0);
  _timer.delay_ms(200);
  _reset.write(1);
  _timer.delay_ms(10);
}
/*
*/
void ZJY096S0700BG01::init()
{
  //reset
  reset();
  
  //config
  write_cmd(0xAE);//--turn off oled panel
  write_cmd(0x00);//---set low column address
  write_cmd(0x10);//---set high column address
  write_cmd(0x40);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
  write_cmd(0x81);//--set contrast control register
  write_cmd(0xCF);// Set SEG Output Current Brightness
  write_cmd(0xA1);//--Set SEG/Column Mapping 
  write_cmd(0xC8);//Set COM/Row Scan Direction   
  write_cmd(0xA6);//--set normal display
  write_cmd(0xA8);//--set multiplex ratio(1 to 64)
  write_cmd(0x3f);//--1/64 duty
  write_cmd(0xD3);//-set display offset	Shift Mapping RAM Counter (0x00~0x3F)
  write_cmd(0x00);//-not offset
  write_cmd(0xd5);//--set display clock divide ratio/oscillator frequency
  write_cmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
  write_cmd(0xD9);//--set pre-charge period
  write_cmd(0xF1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  write_cmd(0xDA);//--set com pins hardware configuration
  write_cmd(0x12);
  write_cmd(0xDB);//--set vcomh
  write_cmd(0x40);//Set VCOM Deselect Level
  write_cmd(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
  write_cmd(0x00);//
  write_cmd(0x8D);//--set Charge Pump enable/disable
  write_cmd(0x14);//--set(0x10) disable
  write_cmd(0xA4);// Disable Entire Display On (0xa4/0xa5)
  write_cmd(0xA6);// Disable Inverse Display On (0xa6/a7) 
  clear();
}
/*
*/
void ZJY096S0700BG01::clear()
{
  set_pos(0, 7, 0, 127);
  for(int i=0; i<8; i++) {
    for(int j=0; j<128; j++) {
      write_data(0x00);
    }
  }
}
/*
 x:0~7
 y:0~127
*/
void ZJY096S0700BG01::set_pos(int x0, int x1, int y0, int y1)
{
  //page
  write_cmd(0x22);
  write_cmd(x0);
  write_cmd(x1);
  //column
  write_cmd(0x21);
  write_cmd(y0);
  write_cmd(y1);
}
/*
*/
void ZJY096S0700BG01::print(print_info_t &info)
{
  uint8_t *data = (uint8_t *)info.data;
  set_pos(info.x0, info.x1, info.y0, info.y1);
  if(data == 0) {
    for(int i=0; i<info.size; i++) {
      write_data(0);
    }
  } else {
    for(int i=0; i<info.size; i++) {
      write_data(data[i]);
    }
  }
}





